package com.tcl.userservice.controller;


import com.tcl.api.request.UserAddRequest;
import com.tcl.api.response.ResultMsg;
import com.tcl.api.response.UserResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.Validate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    //正常接口
    @GetMapping("/get")
    public UserResponse get(@RequestParam("id") Integer id) {
        UserResponse userResponse = new UserResponse();
        userResponse.setId(id);
        userResponse.setName("name_" + id);
        userResponse.setGender(id);
        return userResponse;
    }

    //post方式，form表单提交
    @PostMapping("/get2")
    public UserResponse get2(Integer id) {
        UserResponse userResponse = new UserResponse();
        userResponse.setId(id);
        userResponse.setName("name_" + id);
        userResponse.setGender(id);
        return userResponse;
    }

    //返回带有泛型的包装类
    @GetMapping("/get3")
    public ResultMsg get3(@RequestParam("id") Integer id) {
        UserResponse userResponse = new UserResponse();
        userResponse.setId(id);
        userResponse.setName("name_" + id);
        userResponse.setGender(id);

        ResultMsg resultMsg = new ResultMsg();
        resultMsg.setData(userResponse);
        return resultMsg;
    }

    //===============================================================
    //正常接口
    @GetMapping("/list")
    public List<UserResponse> list(@RequestParam(value = "name", required = false) String name,
                                   @RequestParam(value = "gender", required = false) Integer gender) {
        log.info("name:{},gender:{}", name, gender);
        return list();
    }

    //GET 方式使用form表单
    @GetMapping("/list2")
    public List<UserResponse> list2(UserAddRequest request) {
        log.info("request :{}", request);
        return list();
    }


    private List<UserResponse> list() {
        List<UserResponse> users = new ArrayList<>();
        for (int id = 1; id <= 3; id++) {
            UserResponse userResponse = new UserResponse();
            userResponse.setId(id);
            userResponse.setName("name_" + id);
            userResponse.setGender(id);
            users.add(userResponse);
        }
        return users;
    }

    //===============================================================//

    //正常接口的post接口
    @PostMapping("/add")
    public UserAddRequest add(@RequestBody UserAddRequest request) {
        log.info("request :{}", request);
        return request;
    }

    //POST方式使用form表单
    @PostMapping("/add2")
    public UserAddRequest add2(UserAddRequest request) {
        log.info("request :{}", request);
        return request;
    }

    //============================异常测试=================

    //抛出异常
    @GetMapping("/error/message")
    public ResultMsg error(@RequestParam("id") Integer id) {
        Validate.isTrue(id > 10000, "id 10000!!!!");
        return new ResultMsg();
    }

}
